{
  "cells": [
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "023d771c",
      "metadata": {
        "id": "023d771c"
      },
      "source": [
        "[![Open nbviewer](https://raw.githubusercontent.com/pinecone-io/examples/master/assets/nbviewer-shield.svg)](https://nbviewer.org/github/pinecone-io/examples/blob/master/docs/quick-tour/hello-pinecone.ipynb)"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "conceptual-belfast",
      "metadata": {
        "id": "conceptual-belfast",
        "papermill": {
          "duration": 0.028714,
          "end_time": "2021-04-16T15:08:30.639231",
          "exception": false,
          "start_time": "2021-04-16T15:08:30.610517",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "# Hello, Pinecone!\n",
        "\n",
        "This notebook will walk through the steps to get a simple Pinecone index up and running on AWS.\n"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "first-affairs",
      "metadata": {
        "id": "first-affairs",
        "papermill": {
          "duration": 0.025469,
          "end_time": "2021-04-16T15:08:30.690710",
          "exception": false,
          "start_time": "2021-04-16T15:08:30.665241",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "## Prerequisites"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "banned-huntington",
      "metadata": {
        "id": "banned-huntington",
        "papermill": {
          "duration": 0.023078,
          "end_time": "2021-04-16T15:08:30.737798",
          "exception": false,
          "start_time": "2021-04-16T15:08:30.714720",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "Install dependencies."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "parallel-detective",
      "metadata": {
        "execution": {
          "iopub.execute_input": "2021-04-16T15:08:30.790085Z",
          "iopub.status.busy": "2021-04-16T15:08:30.789322Z",
          "iopub.status.idle": "2021-04-16T15:08:48.879840Z",
          "shell.execute_reply": "2021-04-16T15:08:48.880935Z"
        },
        "id": "parallel-detective",
        "papermill": {
          "duration": 18.119125,
          "end_time": "2021-04-16T15:08:48.881643",
          "exception": false,
          "start_time": "2021-04-16T15:08:30.762518",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [],
      "source": [
        "!pip install -qU \\\n",
        "  pinecone-client==2.2.2 \\\n",
        "  pandas==2.0.3"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "outer-cartridge",
      "metadata": {
        "id": "outer-cartridge",
        "papermill": {
          "duration": 0.022118,
          "end_time": "2021-04-16T15:08:48.932208",
          "exception": false,
          "start_time": "2021-04-16T15:08:48.910090",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "Set up Pinecone. Get your Pinecone API key [here](https://www.pinecone.io/start)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "labeled-specialist",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "execution": {
          "iopub.execute_input": "2021-04-16T15:08:48.980520Z",
          "iopub.status.busy": "2021-04-16T15:08:48.979710Z",
          "iopub.status.idle": "2021-04-16T15:08:49.296796Z",
          "shell.execute_reply": "2021-04-16T15:08:49.296152Z"
        },
        "id": "labeled-specialist",
        "outputId": "7d75712c-5f8b-45fd-f743-bdd50896e252",
        "papermill": {
          "duration": 0.344059,
          "end_time": "2021-04-16T15:08:49.297021",
          "exception": false,
          "start_time": "2021-04-16T15:08:48.952962",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "/Users/jamesbriggs/opt/anaconda3/envs/ml/lib/python3.9/site-packages/pinecone/index.py:4: TqdmExperimentalWarning: Using `tqdm.autonotebook.tqdm` in notebook mode. Use `tqdm.tqdm` instead to force console mode (e.g. in jupyter console)\n",
            "  from tqdm.autonotebook import tqdm\n"
          ]
        }
      ],
      "source": [
        "import os\n",
        "from pinecone import Pinecone\n",
        "\n",
        "# get api key from app.pinecone.io\n",
        "api_key = os.environ.get('PINECONE_API_KEY') or 'YOUR_PINECONE_API_KEY'\n",
        "# find your environment next to the api key in pinecone console\n",
        "env = os.environ.get('PINECONE_ENVIRONMENT') or 'YOUR_PINECONE_ENVIRONMENT'\n",
        "\n",
        "pinecone.init(\n",
        "    api_key=api_key,\n",
        "    environment=env\n",
        ")"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "forbidden-indication",
      "metadata": {
        "id": "forbidden-indication",
        "papermill": {
          "duration": 0.020515,
          "end_time": "2021-04-16T15:08:49.925687",
          "exception": false,
          "start_time": "2021-04-16T15:08:49.905172",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "## Pinecone quickstart\n",
        "\n",
        "With Pinecone you can create a vector index where you can store and search through your vectors."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "EA2EcZsCoWS3",
      "metadata": {
        "id": "EA2EcZsCoWS3",
        "tags": [
          "parameters"
        ]
      },
      "outputs": [],
      "source": [
        "# Giving our index a name\n",
        "index_name = \"hello-pinecone\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "774014f4",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "[]"
            ]
          },
          "execution_count": 3,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "pinecone.list_indexes().names()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "id": "synthetic-essex",
      "metadata": {
        "execution": {
          "iopub.execute_input": "2021-04-16T15:08:49.974096Z",
          "iopub.status.busy": "2021-04-16T15:08:49.972856Z",
          "iopub.status.idle": "2021-04-16T15:08:50.392796Z",
          "shell.execute_reply": "2021-04-16T15:08:50.391552Z"
        },
        "id": "synthetic-essex",
        "papermill": {
          "duration": 0.446682,
          "end_time": "2021-04-16T15:08:50.393195",
          "exception": false,
          "start_time": "2021-04-16T15:08:49.946513",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [],
      "source": [
        "# Delete the index, if an index of the same name already exists\n",
        "if index_name in pinecone.list_indexes().names():\n",
        "    pinecone.delete_index(index_name)"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "94LRI2H8Ch2B",
      "metadata": {
        "id": "94LRI2H8Ch2B",
        "papermill": {
          "duration": 0.021764,
          "end_time": "2021-04-16T15:08:50.446400",
          "exception": false,
          "start_time": "2021-04-16T15:08:50.424636",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "Creating a Pinecone Index."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "id": "4YwC8livCrn2",
      "metadata": {
        "execution": {
          "iopub.execute_input": "2021-04-16T15:08:50.497478Z",
          "iopub.status.busy": "2021-04-16T15:08:50.496767Z",
          "iopub.status.idle": "2021-04-16T15:09:04.224132Z",
          "shell.execute_reply": "2021-04-16T15:09:04.223529Z"
        },
        "id": "4YwC8livCrn2",
        "papermill": {
          "duration": 13.756687,
          "end_time": "2021-04-16T15:09:04.224466",
          "exception": false,
          "start_time": "2021-04-16T15:08:50.467779",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [],
      "source": [
        "import time\n",
        "\n",
        "dimensions = 3\n",
        "pinecone.create_index(\n",
        "    name=index_name,\n",
        "    dimension=dimensions,\n",
        "    metric=\"cosine\"\n",
        ")\n",
        "\n",
        "# wait for index to be ready before connecting\n",
        "while not pinecone.describe_index(index_name).status['ready']:\n",
        "    time.sleep(1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "toy-VhU4LO_O",
      "metadata": {
        "execution": {
          "iopub.execute_input": "2021-04-16T15:09:04.283700Z",
          "iopub.status.busy": "2021-04-16T15:09:04.282289Z",
          "iopub.status.idle": "2021-04-16T15:09:05.096982Z",
          "shell.execute_reply": "2021-04-16T15:09:05.096019Z"
        },
        "id": "toy-VhU4LO_O",
        "papermill": {
          "duration": 0.846255,
          "end_time": "2021-04-16T15:09:05.097384",
          "exception": false,
          "start_time": "2021-04-16T15:09:04.251129",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [],
      "source": [
        "index = pinecone.Index(index_name=index_name)"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "j1F8SLx6C2HH",
      "metadata": {
        "id": "j1F8SLx6C2HH",
        "papermill": {
          "duration": 0.023037,
          "end_time": "2021-04-16T15:09:05.153116",
          "exception": false,
          "start_time": "2021-04-16T15:09:05.130079",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "We have the index ready. Now we will create some simple vectors that will serve as our examples."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "indirect-lafayette",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 112
        },
        "execution": {
          "iopub.execute_input": "2021-04-16T15:09:05.206498Z",
          "iopub.status.busy": "2021-04-16T15:09:05.205699Z",
          "iopub.status.idle": "2021-04-16T15:09:05.404333Z",
          "shell.execute_reply": "2021-04-16T15:09:05.403743Z"
        },
        "id": "indirect-lafayette",
        "outputId": "5bd49b0e-0187-4de2-e564-1d41c61b7bc9",
        "papermill": {
          "duration": 0.227373,
          "end_time": "2021-04-16T15:09:05.404700",
          "exception": false,
          "start_time": "2021-04-16T15:09:05.177327",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>id</th>\n",
              "      <th>vector</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>A</td>\n",
              "      <td>[1.0, 1.0, 1.0]</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>B</td>\n",
              "      <td>[1.0, 2.0, 3.0]</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "  id           vector\n",
              "0  A  [1.0, 1.0, 1.0]\n",
              "1  B  [1.0, 2.0, 3.0]"
            ]
          },
          "execution_count": 7,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "import pandas as pd\n",
        "\n",
        "df = pd.DataFrame(\n",
        "    data={\n",
        "        \"id\": [\"A\", \"B\"],\n",
        "        \"vector\": [[1., 1., 1.], [1., 2., 3.]]\n",
        "    })\n",
        "df"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "oiJKXWxoDjhK",
      "metadata": {
        "id": "oiJKXWxoDjhK",
        "papermill": {
          "duration": 0.022968,
          "end_time": "2021-04-16T15:09:05.452054",
          "exception": false,
          "start_time": "2021-04-16T15:09:05.429086",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "We perform upsert operations in our index. This call will insert a new vector in the index or update the vector if the id was already present."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "id": "efficient-parliament",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "execution": {
          "iopub.execute_input": "2021-04-16T15:09:05.506668Z",
          "iopub.status.busy": "2021-04-16T15:09:05.505145Z",
          "iopub.status.idle": "2021-04-16T15:09:06.180038Z",
          "shell.execute_reply": "2021-04-16T15:09:06.179012Z"
        },
        "id": "efficient-parliament",
        "outputId": "0d9fbac4-4f8a-421e-95a9-0f441d2dcc16",
        "papermill": {
          "duration": 0.704503,
          "end_time": "2021-04-16T15:09:06.180549",
          "exception": false,
          "start_time": "2021-04-16T15:09:05.476046",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'upserted_count': 2}"
            ]
          },
          "execution_count": 8,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "index.upsert(vectors=zip(df.id, df.vector))  # insert vectors"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "id": "enclosed-performer",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "execution": {
          "iopub.execute_input": "2021-04-16T15:09:06.242684Z",
          "iopub.status.busy": "2021-04-16T15:09:06.241999Z",
          "iopub.status.idle": "2021-04-16T15:09:06.350759Z",
          "shell.execute_reply": "2021-04-16T15:09:06.351713Z"
        },
        "id": "enclosed-performer",
        "outputId": "5b67ec13-6863-4b1a-ac45-b57c569923ee",
        "papermill": {
          "duration": 0.140473,
          "end_time": "2021-04-16T15:09:06.352169",
          "exception": false,
          "start_time": "2021-04-16T15:09:06.211696",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'dimension': 3,\n",
              " 'index_fullness': 0.0,\n",
              " 'namespaces': {'': {'vector_count': 2}},\n",
              " 'total_vector_count': 2}"
            ]
          },
          "execution_count": 9,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "index.describe_index_stats()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "id": "leading-shape",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "execution": {
          "iopub.execute_input": "2021-04-16T15:09:06.422440Z",
          "iopub.status.busy": "2021-04-16T15:09:06.420935Z",
          "iopub.status.idle": "2021-04-16T15:09:08.564221Z",
          "shell.execute_reply": "2021-04-16T15:09:08.563202Z"
        },
        "id": "leading-shape",
        "outputId": "fb512e95-ebf4-4e1d-b9c7-74afc3cdd0c2",
        "papermill": {
          "duration": 2.177493,
          "end_time": "2021-04-16T15:09:08.564594",
          "exception": false,
          "start_time": "2021-04-16T15:09:06.387101",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'matches': [{'id': 'A', 'score': 1.0, 'values': [1.0, 1.0, 1.0]}],\n",
              " 'namespace': ''}"
            ]
          },
          "execution_count": 11,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "index.query(\n",
        "    vector=[2., 2., 2.],\n",
        "    top_k=1,\n",
        "    include_values=True\n",
        ") # returns top_k matches"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "z5jcU5_SLMFC",
      "metadata": {
        "id": "z5jcU5_SLMFC",
        "papermill": {
          "duration": 0.035627,
          "end_time": "2021-04-16T15:09:08.629172",
          "exception": false,
          "start_time": "2021-04-16T15:09:08.593545",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "## Delete the Index\n",
        "Delete the index once you are sure that you do not want to use it anymore. Once the index is deleted, you cannot use it again."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "id": "indian-broadcast",
      "metadata": {
        "execution": {
          "iopub.execute_input": "2021-04-16T15:09:08.698129Z",
          "iopub.status.busy": "2021-04-16T15:09:08.697250Z",
          "iopub.status.idle": "2021-04-16T15:09:21.171092Z",
          "shell.execute_reply": "2021-04-16T15:09:21.170231Z"
        },
        "id": "indian-broadcast",
        "papermill": {
          "duration": 12.505772,
          "end_time": "2021-04-16T15:09:21.171527",
          "exception": false,
          "start_time": "2021-04-16T15:09:08.665755",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [],
      "source": [
        "pinecone.delete_index(index_name)"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "e84d82ee",
      "metadata": {},
      "source": [
        "---"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.9.12"
    },
    "papermill": {
      "default_parameters": {},
      "duration": 52.201461,
      "end_time": "2021-04-16T15:09:21.730976",
      "environment_variables": {},
      "exception": null,
      "input_path": "/notebooks/quick_tour/hello_pinecone.ipynb",
      "output_path": "/notebooks/tmp/quick_tour/hello_pinecone.ipynb",
      "parameters": {},
      "start_time": "2021-04-16T15:08:29.529515",
      "version": "2.3.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
